﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Columns;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms.Control.Reservation
{
    public partial class TodayReservationEmployee : BaseUserControl
    {
        /// <summary>
        /// 当前绑定的数据集
        /// </summary>
        private DataTable dtReservationEmployee = new DataTable();

        private DateTime queryTimeReservation = DateTime.Now;

        /// <summary>
        /// 显示时间字段
        /// </summary>
        private const string FIELDNAME_TODAYHOUR = "TODAYHOUR";
        /// <summary>
        /// 小时
        /// </summary>
        private const string FIELDNAME_TOHOUR = "TOHOUR";
        /// <summary>
        /// 分钟
        /// </summary>
        private const string FIELDNAME_TOMINUTE = "TOMINUTE";
        /// <summary>
        /// 显示批注
        /// </summary>
        private const string FIELDNAME_MEMOTIP = "TOSHOWMEMO";

        /// <summary>
        /// 今天的预约
        /// </summary>
        private DataSet dsReservation = new DataSet();

        public TodayReservationEmployee()
        {
            InitializeComponent();
            initDataSet();
        }
        /// <summary>
        /// 数据绑定
        /// </summary>`
        /// <param name="dtEmployee">员工的数据集</param>
        /// <param name="timeReservation">预约时间</param>
        /// <param name="dsReservation">当前预约的数据集</param>
        public void DataBind(DataTable dtEmployee, DateTime timeReservation, DataSet dsReservation)
        {
            string error = string.Empty;
            //获取休假记录
            DataSet dsVacation;
            if (!BussinessProxyManager.Instance.QueryEmployeeVacationDay(queryTimeReservation.ToLongDateString(), string.Empty, out dsVacation, out error))
            {
                MsgBox.MsgInfo("获取员工休假出错：" + error);
                return;
            }

            //当前预约查询时间
            this.queryTimeReservation = new DateTime(timeReservation.Ticks);

            DataRow[] drEmployees = dtEmployee.Select("");
            int idx = 1;
            foreach (DataRow drColumn in drEmployees)
            {
                //员工ID-员工编码 成为列
                string columnFieldName = drColumn[DBEntityEmployee.FieldName.EMPLOYEEID].ToString()
                    + "-" + drColumn[DBEntityEmployee.FieldName.ECODE].ToString();

                // 员工姓名[编码] 显示列
                string columnCaption = drColumn[DBEntityEmployee.FieldName.ENAME].ToString()
                    + "[" + drColumn[DBEntityEmployee.FieldName.ECODE].ToString() + "]";

                //每一个员工新增一列
                dtReservationEmployee.Columns.Add(new DataColumn(columnFieldName, typeof(string)));

                DataRow[] drEmployeeVacation = dsVacation.Tables[0].Select(DBEntityEmoloyeeVacation.FieldName.EMPLOYEEID + " = " + drColumn[DBEntityEmployee.FieldName.EMPLOYEEID].ToString());
                if (drEmployeeVacation.Length > 0)
                {//有员工休假更新数据
                    foreach (DataRow drReservationEmployee in dtReservationEmployee.Rows)
                    {
                        drReservationEmployee[columnFieldName] = "休假";
                    }
                    dtReservationEmployee.AcceptChanges();
                }
                //过滤出员工当天的预约记录
                DataRow[] drEmployeeResverations = dsReservation.Tables[0].Select(DBEntityReservationServiceItem.FieldName.EMPLOYEEID + " = " + drColumn[DBEntityEmployee.FieldName.EMPLOYEEID].ToString());
                foreach (DataRow drReservation in drEmployeeResverations)
                {
                    BindEmployeeReservation(columnFieldName, dtReservationEmployee, drReservation);
                }
                GridColumn gdColumn = new GridColumn();
                gdColumn.Name = columnFieldName;
                gdColumn.FieldName = columnFieldName;
                gdColumn.Caption = columnCaption;
                gdColumn.VisibleIndex = idx;
                gdColumn.Options = ColumnOptions.NonEditable | ColumnOptions.ReadOnly | ColumnOptions.CanResized;
                idx++;
                //gdColumn.
                gvEmployee.Columns.Add(gdColumn);

            }
            //锁定首行
            grdColTime.Fixed = FixedStyle.Left;
            gdEmployee.DataSource = dtReservationEmployee;
            //grdColTime.Style.BackColor = Color.FromArgb(224, 224, 224);            
        }


        public void SetEmployeeTime(int employeeID, DateTime beginTime, DateTime endTime)
        {

        }

        public void ClearEmployeeTime(int employeeID, DateTime beginTime, DateTime endTime)
        {

        }

        private void initDataSet()
        {
            dtReservationEmployee.Columns.Add(new DataColumn(FIELDNAME_TODAYHOUR, typeof(string)));
            dtReservationEmployee.Columns.Add(new DataColumn(FIELDNAME_TOHOUR, typeof(int)));
            dtReservationEmployee.Columns.Add(new DataColumn(FIELDNAME_TOMINUTE, typeof(int)));
            dtReservationEmployee.Columns.Add(new DataColumn(FIELDNAME_MEMOTIP, typeof(string)));
            //将8-22点插入记录中
            for (int i = 8; i < 23; i++)
            {
                DataRow drNew = dtReservationEmployee.NewRow();
                drNew[FIELDNAME_TODAYHOUR] = i.ToString() + ":00";
                drNew[FIELDNAME_TOHOUR] = i;
                drNew[FIELDNAME_TOMINUTE] = 0;
                dtReservationEmployee.Rows.Add(drNew);

                drNew = dtReservationEmployee.NewRow();
                drNew[FIELDNAME_TODAYHOUR] = i.ToString() + ":15";
                drNew[FIELDNAME_TOHOUR] = i;
                drNew[FIELDNAME_TOMINUTE] = 15;
                dtReservationEmployee.Rows.Add(drNew);

                drNew = dtReservationEmployee.NewRow();
                drNew[FIELDNAME_TODAYHOUR] = i.ToString() + ":30";
                drNew[FIELDNAME_TOHOUR] = i;
                drNew[FIELDNAME_TOMINUTE] = 30;
                dtReservationEmployee.Rows.Add(drNew);

                drNew = dtReservationEmployee.NewRow();
                drNew[FIELDNAME_TODAYHOUR] = i.ToString() + ":45";
                drNew[FIELDNAME_TOHOUR] = i;
                drNew[FIELDNAME_TOMINUTE] = 45;
                dtReservationEmployee.Rows.Add(drNew);
            }
        }

        private void gvEmployee_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            Rectangle r = e.Bounds;
            Brush ellipseBrush;
            if (e.Column.Name == "grdColTime")
            {
                //画时间
                int currHour = DateTime.Now.Hour;
                int currMinute = DateTime.Now.Minute;

                int iHour = Convert.ToInt16(e.DisplayText.Split(':')[0]);
                int iMonute = Convert.ToInt16(e.DisplayText.Split(':')[1]);

                ellipseBrush = Brushes.LightCyan;
                if (queryTimeReservation.ToShortDateString().Equals(DateTime.Now.ToShortDateString()) && iHour == currHour)
                {//当前时间当天
                    ellipseBrush = Brushes.LightSkyBlue;
                }
                else
                {
                    int result = 0;
                    Math.DivRem(iHour, 2, out result);
                    if (result == 0)
                    {
                        ellipseBrush = Brushes.LightGoldenrodYellow;
                    }
                }
            }
            else
            {
                //画预约记录
                ellipseBrush = Brushes.White;
                if (e.CellValue == null || e.CellValue.ToString().Equals(string.Empty))
                {
                    return;
                }
                Color drawColor = DBEntityReservation.CUSTOMFUNC.GetColorResverationState(e.CellValue.ToString());
                ellipseBrush = new SolidBrush(drawColor);
            }
            
            e.Graphics.FillRectangle(ellipseBrush, r);
            e.Style.DrawString(e.Graphics, e.DisplayText, r);
            e.Handled = true;
        }
        /// <summary>
        /// 绑定数据集
        /// </summary>
        /// <param name="reservationFieldName">当前预约员工的字段</param>
        /// <param name="dtReservation">预约显示的数据集</param>
        /// <param name="drEmployeeReservation">员工的预约记录</param>
        private void BindEmployeeReservation(string reservationFieldName, DataTable dtReservation, DataRow drEmployeeReservation)
        {
            //计算时段
            DateTime startTime = Convert.ToDateTime(drEmployeeReservation[DBEntityReservation.FieldName.RWORKSTARTTIME]);//换算为最近的一个
            int iStartMinute = DBEntityReservation.CUSTOMFUNC.GetRoundMinute(startTime.Minute);
            int iStartHour = startTime.Hour;

            DateTime endTime = Convert.ToDateTime(drEmployeeReservation[DBEntityReservation.FieldName.RWORKENDTIME]);//换算为最远的一个
            int iEndTime = endTime.Hour * 100 + DBEntityReservation.CUSTOMFUNC.GetRoundMinute(endTime.Minute);
            string startTimeFilter = FIELDNAME_TOHOUR + " >= " + iStartHour + " AND " + FIELDNAME_TOMINUTE + " >= " + iStartMinute;
            string resverationState = DBEntityReservation.CUSTOMFUNC.GetResverationStateStr(drEmployeeReservation[DBEntityReservation.FieldName.RSTATE].ToString());

            //过滤开始时间
            DataRow[] drReservations = dtReservation.Select(startTimeFilter);
            foreach (DataRow dr in drReservations)
            {
                int iReservatioinEndTime = Convert.ToInt32(dr[FIELDNAME_TOHOUR]) * 100 + Convert.ToInt32(dr[FIELDNAME_TOMINUTE]);
                if (iReservatioinEndTime > iEndTime)
                {
                    //超过时间退出
                    break;
                }
                //预约完成C0A(浅蓝色) 预约过期 C0D(钱粉色) 预约进行中 C0N(浅绿色)
                dr[reservationFieldName] = resverationState;
                //批注 
                dr[FIELDNAME_MEMOTIP] = drEmployeeReservation[DBEntityReservation.FieldName.RMEMO].ToString();
            }
        }
       

        private void gvEmployee_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {

        }
    }
}
